package com.mbl.base.modular.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mbl.base.core.common.controller.BaseController;
import com.mbl.base.core.common.dto.MblResult;
import com.mbl.base.core.modular.express.ExpressService;
import com.mbl.base.core.utils.UtilHttp;
import com.mbl.base.modular.mall.entity.Order;
import com.mbl.base.modular.mall.entity.OrderGoods;
import com.mbl.base.modular.mall.service.IOrderGoodsService;
import com.mbl.base.modular.mall.service.IOrderService;
import com.mbl.base.modular.mall.vo.UserVo;
import com.mbl.base.modular.member.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author lwj
 * @since 2020-03-21
 */
@RestController
@RequestMapping("/mall/order")
public class OrderController extends BaseController<Order, IOrderService> {

    @Autowired
    private ExpressService expressService;
    @Autowired
    private IOrderGoodsService orderGoodsService;
    @Autowired
    private IUserService userService;
    /**
     * 查询物流公司
     *
     * @return
     */
    @GetMapping("/channel")
    public Object channel() {
        return MblResult.success(expressService.getVendors());
    }

    @Override
    public MblResult view(Long id) {
        Order order = getService().getById(id);
        List<OrderGoods> orderGoods = orderGoodsService.queryByOrderid(id);
        UserVo user = userService.findUserVoById(order.getUserId());
        Map<String, Object> data = new HashMap<>();
        data.put("order", order);
        data.put("orderGoods", orderGoods);
        data.put("user", user);
        return MblResult.success(data);
    }


    @Override
    public void machiningList(QueryWrapper<Order> queryWrapper, String condition) {
        final String orderSn = UtilHttp.getPara("orderSn");
        queryWrapper.eq(StringUtils.isNotBlank(orderSn), "order_sn", orderSn);
        final String userId = UtilHttp.getPara("userId");
        queryWrapper.eq(StringUtils.isNotBlank(userId), "user_id", userId);
        final String[] orderStatusArrays = UtilHttp.getParaArray("orderStatusArray");
        queryWrapper.in(orderStatusArrays != null && orderStatusArrays.length > 0, "order_status", orderStatusArrays);
        final String addTimeStr = UtilHttp.getPara("addTimeStr");
        final String addTimeEnd = UtilHttp.getPara("addTimeEnd");
        queryWrapper.between(StringUtils.isNoneBlank(addTimeStr, addTimeEnd), "create_time", addTimeStr, addTimeEnd);
        super.machiningList(queryWrapper, condition);
    }
}

